# include <bits/stdc++.h>
# define N 8005
using namespace std;
int n,m;
int a[N],x[N],y[N];
bool isp[60005];
bool ok[60005];
int dis(int i,int j)
{
    return abs(x[i]-x[j])+abs(y[i]-y[j]);
}
void solve()
{
    cin>>n>>m;
    long long ans=0;
    for(int i=1;i<=n;i++)
        cin>>x[i]>>y[i];
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++,cerr<<i<<endl)
        for(int j=i+1;j<=n;j++)
            for(int k=j+1;k<=n;k++)
            {
                int a1=dis(i,j),a2=dis(i,k),a3=dis(j,k);
                int now=a1+a2+a3-max({a1,a2,a3})-min({a1,a2,a3});
                if(ok[now])
                    ans+=a[i]+a[j]+a[k];
            }
    cout<<ans<<endl;
    return ;
}
int main()
{
    freopen("chess.in","r",stdin);
    freopen("chess.out","w",stdout);
    memset(isp,1,sizeof isp);
    isp[1]=0;
    for(int i=2;i<=60000;i++)
        if(isp[i])
        {
            ok[i]=(i<=10 || i%20==3);
            for(long long j=2;i*j<=60000;j++)
                isp[i*j]=0;
        }
    int T;
    cin>>T;
    while(T--)
        solve();
    return 0;
}